package com.woniuxy.microgoodsserver.goodmag.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.microcommonserver.bean.gy.GoodsAttributesInfo;
import com.woniuxy.microcommonserver.bean.gy.GoodsInfo;
import com.woniuxy.microcommonserver.pageHelper.PageRequest;
import com.woniuxy.microgoodsserver.goodmag.mapper.GoodMapper;
import com.woniuxy.microgoodsserver.goodmag.service.GoodService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**商品管理实现类
 * @author ：gouyang
 * @description：TODO
 * @date ：2021/1/26 9:50
 */
@Service
@Transactional(readOnly = true)
public class GoodServiceImpl implements GoodService {

    @Resource
    private GoodMapper goodMapper;
    /**
        新增商品
     * @params:
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public void addGood(GoodsInfo goodsInfo) {
        //添加商品,并返回ID复制到javaBean中
        goodMapper.addGood(goodsInfo);
        System.out.println(goodsInfo.getId());
        //添加商品sku
        goodsInfo.getGoodsAttributesInfos().forEach(e->{
            //添加商品sku数据库
            goodMapper.addGoodsAttributesInfos(e,goodsInfo.getId());
        });

    }
    /**
     查询所有商品信息
     * @params:
     * @return
     */
    @Override
    public PageInfo<List<Map>> findAllGoods(PageRequest pageRequest, GoodsInfo goodsInfo) {
        return PageHelper.startPage(pageRequest.getPageNum(),pageRequest.getLimit()).doSelectPageInfo(()->{
             goodMapper.findAllGoods(goodsInfo);
        });
    }
    /**
        根据ID删除商品信息
     * @params:
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public void deleteGood(Integer id) {
        goodMapper.deleteGood(id);
    }



    /**
     修改商品信息包括Sku属性
     * @params:
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public void updateGood(GoodsInfo goodsInfo) {
        //修改商品sku属性
        goodMapper.updateAttribute(goodsInfo.getGoodsAttributesInfos().get(0));

        //修改商品信息
        goodMapper.updateGood(goodsInfo);
    }
    /**
     根据ID查询商品名
     * @params:
     * @return
     */
    @Override
    public GoodsInfo findGoodById(Integer id) {
        return goodMapper.findGoodById(id);
    }

}
